package com.voxel.sense.infomsg.module.task.send.firmlist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.com.common.app.BaseActivity;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.response.FirmNameByIdEntity;

public class FirmSelectListActivity extends BaseActivity {
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.bt_search)
    Button bt_search;
    private FirmPickRecyclerAdapter mAdapter;
    private static int max_count = 5;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_firm_select_list;
    }

    public static void show(Activity context, ArrayList<FirmNameByIdEntity> list) {
        Intent intent = new Intent(context, FirmSelectListActivity.class);
        intent.putParcelableArrayListExtra(IntentConstants.LIST_TYPE, list);
        context.startActivityForResult(intent, IntentConstants.FIRM_SELECT_LIST);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        ArrayList<Parcelable> datas = getIntent().getParcelableArrayListExtra(IntentConstants.LIST_TYPE);
        return datas != null && datas.size() > 0;
    }

    @Override
    protected void initData() {
        super.initData();
        bt_search.setVisibility(View.VISIBLE);
        bt_search.setText("确定");
        ArrayList<FirmNameByIdEntity> datas = getIntent().getParcelableArrayListExtra(IntentConstants.LIST_TYPE);
        ArrayList<FirmNameByIdEntity> selectedDatas = new ArrayList<>();
        for (FirmNameByIdEntity data : datas) {
            if (data.isSelect) {
                selectedDatas.add(data);
            }
        }
        mAdapter = new FirmPickRecyclerAdapter(datas);
        mAdapter.setSeletedDatas(selectedDatas);
        mAdapter.replace(datas);
        recycle_view.setLayoutManager(new LinearLayoutManager(this));
        recycle_view.setAdapter(mAdapter);
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider));
        mAdapter.setMaxCount(max_count);
    }


    @Override
    protected void initEven() {
        super.initEven();
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mAdapter.getFilter().filter(s.toString());
            }
        });
    }

    @OnClick({R.id.btn_search_left, R.id.bt_search})
    void OnClickEvent(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(IntentConstants.LIST_TYPE, (ArrayList<? extends Parcelable>) mAdapter.getSourceList());
        intent.putParcelableArrayListExtra(IntentConstants.EXTRAX_DATA, (ArrayList<? extends Parcelable>) mAdapter.getSeletedDatas());
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

}
